package com.ffcs.ffxtc.modules.base.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ffcs.aop.Loggable;
import com.ffcs.domin.UserInfo;
import com.ffcs.ffxtc.modules.base.dao.DrawdbUserService;
import com.ffcs.ffxtc.modules.base.dao.ShareDataService;
import com.ffcs.ffxtc.modules.base.domin.entity.DrawdbUser;
import com.ffcs.ffxtc.modules.base.domin.entity.ShareData;
import com.ffcs.ffxtc.modules.base.service.UserService;
import com.ffcs.utils.MD5Utils;
import com.ffcs.utils.UserUtils;
import com.ffcs.utils.base.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 张正兴
 * @since 2024-04-30
 */
@RestController
@RequestMapping("/drawdbUser")
@Tag(name = "用户管理表", description = "")
@RequiredArgsConstructor
public class DrawdbUserController {

    private final DrawdbUserService drawdbUserService;

    private final UserService userService;

    private final ShareDataService shareDataService;

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping("/all")
    @Operation(summary = "查询所有")
    public R<List<DrawdbUser>> findAllDrawdbUser() {
        UserInfo userInfo = UserUtils.getUserInfo();
        return R.ok(drawdbUserService.list(new QueryWrapper<DrawdbUser>()
                .ne(DrawdbUser.ID, Long.parseLong(userInfo.getId())
                )));
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据id查询")
    @Parameter(name = "id", description = "id")
    public R<DrawdbUser> findOneDrawdbUser(@PathVariable Long id) {
        return R.ok(drawdbUserService.getById(id));
    }

    /**
     * 根据用户名查询
     *
     * @param name
     * @return
     */
    @GetMapping("/userName/{name}")
    @Operation(summary = "根据有用户名查询")
    @Parameter(name = "name", description = "name")
    public R<DrawdbUser> findOneDrawdbUserName(@PathVariable String name) {
        return R.ok(drawdbUserService.getOne(new QueryWrapper<DrawdbUser>().eq(DrawdbUser.USER_NAME, name)));
    }

    /**
     * 新增
     *
     * @param drawdbUser
     * @return
     */
    @PostMapping("/drawdbUserAdd")
    @Operation(summary = "注册")
    @Loggable("注册新用户")
    public R<Boolean> addDrawdbUser(@RequestBody DrawdbUser drawdbUser) {
        long count = drawdbUserService.count(new QueryWrapper<DrawdbUser>().eq(DrawdbUser.USER_NAME,
                drawdbUser.getUserName()));
        if (count > 0) {
            return R.error("用户名已存在");
        } else {
            drawdbUser.setPassword(MD5Utils.getMD5(drawdbUser.getPassword()));
            return R.ok(drawdbUserService.save(drawdbUser));
        }
    }

    /**
     * 用户登陆
     *
     * @param drawdbUser
     * @return
     */
    @PostMapping("/login")
    @Operation(summary = "登陆")
    public R<String> login(@RequestBody DrawdbUser drawdbUser) {
        return R.ok(userService.login(drawdbUser));
    }

    /**
     * 更新
     *
     * @param drawdbUser
     * @return
     */
    @PutMapping("/updateUser")
    @Operation(summary = "更新")
    public R<Boolean> updateDrawdbUser(@RequestBody DrawdbUser drawdbUser) {
        if (ObjectUtil.isNotEmpty(drawdbUser.getPassword())) {
            drawdbUser.setPassword(MD5Utils.getMD5(drawdbUser.getPassword()));
        }
        return R.ok(drawdbUserService.updateById(drawdbUser));
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/batch")
    @Operation(summary = "删除")
    @Parameter(name = "id", description = "批量ID")
    public R<Boolean> deleteBatchDrawdbUser(List<Long> ids) {
        return R.ok(drawdbUserService.removeByIds(ids));
    }

    /**
     * 分享
     *
     * @param shareData
     * @return
     */
    @PutMapping("/share")
    @Operation(summary = "分享")
    public R<Boolean> share(@RequestBody List<ShareData> shareData) {
        return R.ok(drawdbUserService.share(shareData));
    }

    /**
     * 分享
     *
     * @param dataId
     * @return
     */
    @GetMapping("/share/{dataId}")
    @Operation(summary = "获取分享")
    public R<List<ShareData>> getShare(@PathVariable("dataId") String dataId) {
        UserInfo userInfo = UserUtils.getUserInfo();
        return R.ok(shareDataService.list(new QueryWrapper<ShareData>()
                .eq(ShareData.DRAWDB_DATA_ID, Long.parseLong(dataId))
                .eq(ShareData.DRAWDB_USER_ID, Long.parseLong(userInfo.getId()))
        ));
    }
}